﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Diagnostics;

#endregion


namespace ScrumTable.Common.Logging
{
    /// <summary>
    /// Contains the a logging functionality for critical errors, which should
    /// not abort the application but must be written into a file / logging
    /// console.
    /// </summary>
    /// <typeparam name="T">Specifies the logging category.</typeparam>
    public class CriticalLogger<T>
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private const string ErrorFormatString = "{0} -> {1}: {2}";

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Logs a critical error to the debug output if a debugger is attached.
        /// Otherwise the error will be logged to the console.
        /// </summary>
        /// <param name="message">Specifies the detailed error message.</param>
        /// <param name="error">Specifies the thrown exception.</param>
        public static void Log(string message, Exception error = null)
        {
            LogToDebugConsole(message, error);

            if (error != null) { Logger<T>.Error(message, error); }
            else { Logger<T>.Error(message); }
        }

        /// <summary>
        /// Logs a critical error to the debug output if a debugger is attached.
        /// Otherwise the error will be logged to the console.
        /// </summary>
        /// <param name="message">Specifies the detailed error message.</param>
        /// <param name="error">Specifies the thrown exception.</param>
        public static void LogToDebugConsole(string message, Exception error = null)
        {
            if (Debugger.IsAttached && Debugger.IsLogging())
            {
                Debugger.Log((int)TraceLevel.Error, typeof(T).FullName, GetErrorString(message, error) + Environment.NewLine);
            }
            else if (Debug.Listeners.Count > 0)
            {
                Debug.WriteLine(GetErrorString(message, error), typeof(T).FullName);
            }
            else
            {
                Console.WriteLine(GetErrorString(message, error));
            }
        }

        /// <summary>
        /// Logs a critical error to the debug output if a debugger is attached.
        /// Otherwise the error will be logged to the console.
        /// </summary>
        /// <param name="message">Specifies the detailed error message with format patterns.</param>
        /// <param name="args">Specifies the arguments which should be inserted into the message.</param>
        public static void LogToDebugConsoleFormat(string message, params string[] args)
        {
            LogToDebugConsole(string.Format(message, args));
        }

        private static string GetErrorString(string message, Exception toGetError)
        {
            if (toGetError != null)
                return string.Format(ErrorFormatString, message, toGetError.GetType(), toGetError.Message);
            return message;
        }

        #endregion
    }
}
